
from sqlalchemy import *

metadata = BoundMetaData('sqlite:///min_db.db')

question_table = Table('questions', metadata,
        Column('question_id', Integer, primary_key=True),

        Column('pre_proc', Unicode),
        Column('post_proc', Unicode),
        Column('position', Integer, unique=True),

        Column('var_name', Unicode, unique=True),
        Column('text', Unicode),
        Column('type', String(32)),

        # type = text_question
        #Column('min_length', Integer)
        )

text_question_table = Table('text_questions', metadata,
        Column('question_id', Integer, ForeignKey('questions.question_id'), primary_key=True),
        Column('min_length', Integer),
        Column('max_length', Integer)
        )


option_table = Table('option_table', metadata,
        Column('option_id', Integer, primary_key=True),
        Column('question_id', Integer, ForeignKey('questions.question_id')),
        Column('option_name', Unicode),
        Column('option_number', Integer, unique=True)
        )


class Question(object):
    visible_fields = ['pre_proc', 'post_proc', 'position', 'var_name', 'text', 'type']
    def __init__(self):
        pass

class Option(object):
    def __init__(self, name, number):
        self.option_name = name
        self.option_number = number
    def __repr__(self):
        return "%s" % (self.option_name)
    pass

class SingleChoice(Question):
    def say_hi(self):
        print self.visible_fields
    def __repr__(self):
        beginning = "%s: %s\n" % (self.var_name, self.text)
        options = "\n".join(["  %d : %s" % (x.option_number, x.option_name) for x in self.options])
        return beginning + options
    pass

class TextQuestion(Question):
    pass

metadata.drop_all()
metadata.engine.echo = True
question_table.create()
text_question_table.create()
#option_table.create()

session = create_session()
question_mapper = mapper(Question, question_table)
mapper(TextQuestion, text_question_table, inherits=question_mapper, polymorphic_identity='TextQuestin')

tq = TextQuestion()


session.save(tq)
session.flush()


#mapper(Option, option_table)
#mapper(SingleChoice, singlechoice_question_table, inherits=question_mapper)
#mapper(SingleChoice, singlechoice_question_table, properties={'options':relation(Option)}, inherits=question_mapper)

#jointable = sql.join(question_table, option_table, question_table.c.question_id == option_table.c.question_id)
#mapper(SingleChoice, question_table, properties={'options':relation(Option)})

#s = SingleChoice()
#s.var_name = "Country"
#s.text = "Where are you from?"
#
#s.options.append( Option('Norway', 1))
#s.options.append( Option('Sweden', 2))
#s.options.append( Option('Denmark', 3))
#s.options.append( Option('Deutchland', 4))
#
#session.save(s)

#print s
#
#session.flush()
#
#
#from apihelper import info
#q = Question()
#q.var_name = "hi world"
#q.min_length = 123
#print q.raff
#print q.min_length
#
#
